package org.example.brand;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.example.pojo.Brand;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 *
 * 品牌数据增删改查
 */
public class BrandTest {
    @Test
    public void testSelectAll() throws Exception {
        // 加载配置文件
        Properties prop = new Properties();
        prop.load(new FileInputStream("src/druid.properties"));

        // 获取连接池对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);

        // 获取数据库连接
        Connection connection = dataSource.getConnection();

        //定义sql
        String sql = "select * from tb_brand";

        //获取pstmt对象
        PreparedStatement pstmt = connection.prepareStatement(sql);

        //执行sql
        ResultSet rs = pstmt.executeQuery();

        //处理结果
        Brand brand = null;
        List<Brand> brands = new ArrayList<>();
        while (rs.next()){
            int id = rs.getInt("id");
            String brandName = rs.getString("brand_name");
            String companyName = rs.getString("company_name");
            int ordered = rs.getInt("ordered");
            String description = rs.getString("description");
            int status = rs.getInt("statu");

            brand = new Brand();
            brand.setId(id);
            brand.setBrandName(brandName);
            brand.setCompanyName(companyName);
            brand.setOrdered(ordered);
            brand.setDescription(description);
            brand.setStatus(status);

            brands.add(brand);
        }
        System.out.println(brands);

        //释放资源
        rs.close();
        pstmt.close();
        connection.close();


    }
}
